package com.thinqq.web.server;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @author rakesh
 *
 */
public class EnquiryServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		Lead ld = new Lead(req.getParameter("name"));
		ld.setOrganization(req.getParameter("orgname"));
		ld.setBusiness(req.getParameter("business"));
		ld.setDesignation(req.getParameter("designation"));
		ld.setDesiredsvc((String[])req.getParameterMap().get("desiredservice"));
		ld.setEmail(req.getParameter("email"));
		ld.setBusinessphone(req.getParameter("businessphone"));
		ld.setContacttime(req.getParameter("contacttime"));
		ld.setAddress(req.getParameter("address"));
		ld.setCountry(req.getParameter("country"));
		ld.setZip(req.getParameter("zip"));
		new DataStoreService().storeLead(ld);
		
		Properties props = new Properties();
		Session session = Session.getDefaultInstance(props, null);

		String msgBody = "We got new lead"+ld.toString();

		try {
			Message msg = new MimeMessage(session);
			//TODO: Change to thinqq cloud admin mail address
			msg.setFrom(new InternetAddress("subbu@gmail.com", "Example.com Admin"));
			msg.addRecipient(Message.RecipientType.TO,new InternetAddress("rakesh@thinqq.com", "Rakesh"));
			msg.addRecipient(Message.RecipientType.TO,new InternetAddress("subramaniam@thinqq.com", "Subbu"));
			msg.setSubject("the new enquiry");
			msg.setText(msgBody);
			Transport.send(msg);
		} catch (AddressException e) {
			// ...
		} catch (MessagingException e) {
			// ...
		}
	}
}
